﻿using System;
using System.Collections.Generic;
using System.Text;
using KPIS.DBM;
using System.Configuration;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_IcmMasResourceItem
    {
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        public IList<M_IcmMasResourceItem> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_IcmMasResourceItem> infoList = new List<M_IcmMasResourceItem>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT icm_res_item_seq,icm_res_item_code,icm_res_item_old_code,icm_res_item_name "
                        +" ,icm_res_item_descr,icm_res_sub_grp_seq,gpsc_code,giin_code,std_price,min_price "
                        +" ,max_price,icm_unit_seq,min_qty,max_qty,image_name,record_status "
                        +" ,concat(COALESCE(icm_res_item_code,''),' ',COALESCE(icm_res_item_name,'')) as icm_res_code_name "
                        +" FROM icm_mas_resource_item "
                        +" WHERE record_status<>'D' ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_IcmMasResourceItem info = new M_IcmMasResourceItem();
                        info.icm_res_item_seq = Convert.ToInt32(drc[i]["icm_res_item_seq"]);
                        info.icm_res_item_code = drc[i]["icm_res_item_code"].ToString();
                        info.icm_res_item_name = drc[i]["icm_res_item_name"].ToString();
                        info.icm_res_item_descr = drc[i]["icm_res_item_descr"].ToString();
                        info.icm_code_name = drc[i]["icm_res_code_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<M_IcmMasResourceItem> GetListFillByName(string icm_res_item_name)
        {
            string strSQL = "";
            DataRowCollection drc = null;
            IList<M_IcmMasResourceItem> infoList = new List<M_IcmMasResourceItem>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT icm_res_item_seq,icm_res_item_code,icm_res_item_old_code,icm_res_item_name "
                        + " ,icm_res_item_descr,icm_res_sub_grp_seq,gpsc_code,giin_code,std_price,min_price "
                        + " ,max_price,icm_unit_seq,min_qty,max_qty,image_name,record_status "
                        + " ,concat(COALESCE(icm_res_item_code,''),' ',COALESCE(icm_res_item_name,'')) as icm_res_code_name "
                        + " FROM icm_mas_resource_item "
                        + " WHERE record_status<>'D' and icm_res_item_name LIKE '%' '" + icm_res_item_name + "'  '%'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_IcmMasResourceItem info = new M_IcmMasResourceItem();
                        info.icm_res_item_seq = Convert.ToInt32(drc[i]["icm_res_item_seq"]);
                        info.icm_res_item_code = drc[i]["icm_res_item_code"].ToString();
                        info.icm_res_item_name = drc[i]["icm_res_item_name"].ToString();
                        info.icm_res_item_descr = drc[i]["icm_res_item_descr"].ToString();
                        info.icm_code_name = drc[i]["icm_res_code_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return infoList;
        }

        public M_IcmMasResourceItem GetListFillBySeq(Nullable<Int32> icm_res_item_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_IcmMasResourceItem info = new M_IcmMasResourceItem();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@icm_res_item_seq", icm_res_item_seq);

                strSQL = " SELECT icm_res_item_seq,icm_res_item_code,icm_res_item_old_code,icm_res_item_name "
                        +" ,icm_res_item_descr,icm_res_sub_grp_seq,gpsc_code,giin_code,std_price,min_price "
                        +" ,max_price,icm_unit_seq,min_qty,max_qty,image_name,record_status "
                        + " ,concat(COALESCE(icm_res_item_code,''),' ',COALESCE(icm_res_item_name,'')) as icm_res_code_name "
                        +" FROM icm_mas_resource_item "
                        + " WHERE record_status<>'D'  and icm_res_item_seq = @icm_res_item_seq ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.icm_res_item_seq = Convert.ToInt32(drc[0]["icm_res_item_seq"]);
                    info.icm_res_item_code = drc[0]["icm_res_item_code"].ToString();
                    info.icm_res_item_name = drc[0]["icm_res_item_name"].ToString();
                    info.icm_res_item_descr = drc[0]["icm_res_item_descr"].ToString();
                    info.icm_code_name = drc[0]["icm_res_code_name"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

    }
}
